//
//  TGRealReachability.m
//  TongGuanLease
//
//  Created by 张奇 on 2022/4/11.
//

#import "TGRealReachability.h"

@implementation TGRealReachability
+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static TGRealReachability *_manager = nil;
    dispatch_once(&onceToken, ^{
        _manager = [[self alloc] init];
    });
    return _manager ;
}
- (instancetype)init {
    if (self = [super init]) {
        GLobalRealReachability.hostForPing = @"www.baidu.com";
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(networkChanged:)
                                                     name:kRealReachabilityChangedNotification
                                                   object:nil];
        ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
        _status = status;
      
    }
    return self;
}

+(BOOL)isReachable {
    
    if ([TGRealReachability shareInstance].status == RealStatusNotReachable) {
        [TGRealReachability shareInstance].status = [GLobalRealReachability currentReachabilityStatus];
        if ([TGRealReachability shareInstance].status!=RealStatusNotReachable) {
            return YES;
        }
        return NO;
    }
    return YES;
}

+ (BOOL)isOpenVPN{
    return [GLobalRealReachability isVPNOn];
}
- (void)networkChanged:(NSNotification *)notification
{
    RealReachability *reachability = (RealReachability *)notification.object;
   _status = [reachability currentReachabilityStatus];
//    ReachabilityStatus previousStatus = [reachability previousReachabilityStatus];
//    NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus));
    if (self.statusBlock) {
        self.statusBlock(_status);
    }
//    if (status == RealStatusNotReachable)
//    {
//        self.flagLabel.text = @"Network unreachable!";
//    }
//    
//    if (status == RealStatusViaWiFi)
//    {
//        self.flagLabel.text = @"Network wifi! Free!";
//    }
//    
//    if (status == RealStatusViaWWAN)
//    {
//        self.flagLabel.text = @"Network WWAN! In charge!";
//    }
//    
//    WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
//    
//    if (status == RealStatusViaWWAN)
//    {
//        if (accessType == WWANType2G)
//        {
//            self.flagLabel.text = @"RealReachabilityStatus2G";
//        }
//        else if (accessType == WWANType3G)
//        {
//            self.flagLabel.text = @"RealReachabilityStatus3G";
//        }
//        else if (accessType == WWANType4G)
//        {
//            self.flagLabel.text = @"RealReachabilityStatus4G";
//        }
//        else
//        {
//            self.flagLabel.text = @"Unknown RealReachability WWAN Status, might be iOS6";
//        }
//    }
}

@end
